<template>
    <view class="container">
        <u-alert title="为保护你的隐私安全，证件照片仅用于实名认证" type ="error" center></u-alert>
        <view class="card">
            <view class="card_item">
                <image class="card_item_sfz" :src="(avatarUrl ? avatarUrl:'../../../pagesC/static/img/avatar.svg')" mode="widthFix"></image>
                <view class="card_item_icon" @click="takePhoto"><u-icon name="camera-fill" size="28" color="#fff"></u-icon></view>

                <!-- <button class="authorization" type="default" open-type="chooseAvatar" @chooseavatar="chooseavatar">上传微信头像</button> -->

                <text class="text1">点击拍摄本人照片</text>
                <text class="text2">此照片将用于闸机打卡，请拍摄清晰以免影响闸机识别</text>
            </view>
        </view>
        <view class="next_btn_box">
            <u-button class="next_btn" type="primary" color="#007aff" @click="handleSubmit">提交</u-button>
        </view>
    </view>
</template>

<script setup>
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { upLoad } from '@/api/qss/zxhapi.js'
import { getUserInfo } from '@/utils/permission.js'
import { personInfoAdd, personInfoAuth} from '@/api/qss/certification.js'

const avatarUrl = ref('') //头像
const form = ref({})
const verifyStatus = ref(1) //校验状态 0校验成功 1校验失败
const token = uni.getStorageSync('token')

/* 选择头像 */
const takePhoto = () => {
    uni.chooseImage({
		count: 1,
		sizeType: ['original', 'compressed'], 
		sourceType: ['camera'], 
		success: function(res) {
			// 判断图片大小

			if (res.tempFiles[0].size / 1024 / 1024 <= 10) {
				const tempFilePaths = res.tempFilePaths;
				wx.cropImage({
					src: tempFilePaths[0], // 图片路径
					cropScale: '1:1', // 裁剪比例
					success: (data) => {
						avatarUrl.value = data.tempFilePath

                        upLoad({path: data.tempFilePath}).then(res => {
                            console.log(res,'res')
                            form.value.avatar = `${res.data.url}?Authorization=Bearer ${token}&platform=corp`
                            uni.setStorageSync('certificationInfo', JSON.stringify(form.value))
                 
                            const autoData = {
                                image: form.value.avatar,
                                idCardNumber: form.value.identityNumber,
                                name: form.value.name,
                                startDate:form.value.issuedTime,
                                endDate:form.value.expiredTime
                            }
                            // uni.showLoading({
                            //     title: '人员校验中...',
                            // })
                            personInfoAuth(autoData).then(res => {
                                if(res.code === 200 && res.data.verifyStatus == 0) {
                                    uni.showToast({
                                        title: '校验成功',
                                        icon: 'success'
                                    })
                                    //校验身份信息是否通过
                                    verifyStatus.value = 0
                                }else {
                                    uni.showToast({
                                        title: '校验失败,请重新上传！',
                                        icon: 'error'
                                    })
                                }
                            }).catch(err => {
                                uni.showToast()({
                                    title: '校验失败,请重新上传！',
                                    icon: 'error'
                                })
                            })
                        })

					},
					fail: (uploadFileErr) => {
						console.error('upload fail:', uploadFileErr);
					}
				})
			} else {
				uni.showToast('上传图片不可超过10M')
			}
		}
	})

}

/* 提交 */


const handleSubmit = () => {
    if(verifyStatus.value == 1) {
        uni.showToast({
            title: '请先校验身份信息',
            icon: 'error'
        })
        return
    }

    // form.value.phone = '15612312312'
    form.value.uuid = new Date().getTime()
    form.value.city = '--'
    form.value.town = '--'
    form.value.province = '--'
    form.value.linkMan = '--'
    form.value.linkManPhone = '--'
    form.value.marriageStatus = '--'
    form.value.identityType = '1'
    form.value.hasMajorDisease = '0'
    form.value.isIdentified = '1'


    form.value.avatar = getImgUrl(form.value.avatar)
    form.value.imageIdentityFront = getImgUrl(form.value.imageIdentityFront)
    form.value.imageIdentityBack = getImgUrl(form.value.imageIdentityBack)


    personInfoAdd(form.value).then(res => {
        if(res.code === 200) {
            getUserInfo().then(res => {
                uni.showToast({
                    title: '提交成功',
                    icon: 'success'
                })
                uni.removeStorageSync('certificationInfo')
                //跳转首页
                uni.switchTab({
                    url: '/pages/index/index'
                })
            }).catch(err => {
                uni.showToast({
                    title: '提交失败',
                    icon: 'error'
                })
            })
           
        }else {
            uni.showToast({
                title: res.msg,
                icon: 'error'
            })
        }
    })
}

//图片路劲去除末尾参数
const getImgUrl = (url) => {
    return url.split('?')[0]
}

onShow(()=> {
    //获取本地存储的认证信息
    if(uni.getStorageSync('certificationInfo')) {
        form.value = JSON.parse(uni.getStorageSync('certificationInfo'))
        console.log(form.value,'form333')
    }
})
</script>

<style lang="scss" scoped>
@import '../../static/style/index.scss';


.container{
    width: 100%;
    .card{
        width: 100%;
        background: #fff;
        border-radius: 10rpx;
        padding: 30rpx 40rpx;
        box-sizing: border-box;
        &_item{
            display: flex;
            // align-items: center;
            justify-content: center;
            width: 100%;
            height: 520rpx;
            background-color: #F8F8F8;
            border-radius: 10rpx;
            position: relative;
            margin-bottom: 30rpx;
            overflow: hidden;
            &_sfz{
                width: 310rpx;
                height: 310rpx;
                margin-top: 80rpx;
            }
            &_icon{
                position: absolute;
                top: 45%;
                left: 50%;
                transform: translate(-50%, -50%);
                width: 100rpx;
                height: 100rpx;
                background-color: #689EFE;
                box-shadow: 0px 1px 6px 0px rgba(0, 0, 0, 0.23);
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            .text1{
                position: absolute;
                font-size: 26rpx;
                color: #999999;
                bottom: 70rpx;
            }
            .text2{
                position: absolute;
                font-size: 26rpx;
                color: #999999;
                bottom: 15rpx;
            }
        }
    }
}
</style>